//
//  UPConstantAndVBScript.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/15.
//

#import "UPConstantAndVBScript.h"
#import "AppConfig.h"

// 登录失效
NSNotificationName const kLogOutOrExpiredNotificationKey = @"logOutOrExpiredNotificationKey";
// 跟进订单
NSNotificationName const kFollowUpMyOrderKey = @"followUpMyOrderKey";
NSString * const latitudeKey = @"latitudeKey";
NSString * const longitudeKey = @"longitudeKey";
// 常量实现
const CGFloat mainTabBarHeight = 83.5;

// 场景类型枚举实现
UPSceneType const UPSceneTypeSignIn = @"1";
UPSceneType const UPSceneTypeIdCredential = @"2";
UPSceneType const UPSceneTypeFaceCredential = @"3";
UPSceneType const UPSceneTypePersonalCredential = @"4";
UPSceneType const UPSceneTypeContactCredential = @"6";
UPSceneType const UPSceneTypeBankCredential = @"7";
UPSceneType const UPSceneTypeStartLoan = @"8";
UPSceneType const UPSceneTypeEndLoan = @"9";

// 状态栏高度
CGFloat statusBarHeight(void) {
    UIWindow *window = [UIApplication up_currentKeyWindow];
    if (@available(iOS 13.0, *)) {
        return window.windowScene.statusBarManager.statusBarFrame.size.height ?: 20.0;
    } else {
        return [UIApplication sharedApplication].statusBarFrame.size.height ?: 20.0;
    }
}

@implementation UIWindow (SafeArea)

+ (UIEdgeInsets)keyWindowsafeAreaInsets {
    if (@available(iOS 11.0, *)) {
        return [UIApplication sharedApplication].windows.firstObject.safeAreaInsets;
    } else {
        return UIEdgeInsetsZero;
    }
}

@end

// 安全区域
CGFloat topSafeArea(void) {
    return [UIWindow keyWindowsafeAreaInsets].top;
}

CGFloat bottomsafeArea(void) {
    return [UIWindow keyWindowsafeAreaInsets].bottom;
}

// 实用函数
NSString *confusionString(void) {
    NSUInteger length = arc4random_uniform(5) + 6;
    NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    NSMutableString *randomString = [NSMutableString stringWithCapacity:length];
    
    for (NSUInteger i = 0; i < length; i++) {
        [randomString appendFormat:@"%C", [letters characterAtIndex:arc4random_uniform((uint32_t)[letters length])]];
    }
    
    return randomString;
}

NSString *LocalizableForKey(NSString *key) {
    // 1. 获取当前语言
    NSString *currentLanguage = @"en";
    if ([AppConfig sharedInstance].appCountryEnv == AppCountryEnvIndonesia) {
        currentLanguage = @"id";
    }
    
    // 2. 尝试加载对应语言的字符串
    NSString *path = [[NSBundle mainBundle] pathForResource:currentLanguage ofType:@"lproj"];
    if (path) {
        NSBundle *languageBundle = [NSBundle bundleWithPath:path];
        NSString *localizedString = [languageBundle localizedStringForKey:key value:nil table:nil];
        if (![localizedString isEqualToString:key]) {
            return localizedString;
        }
    }
    
    // 3. 回退到英语
    if (![currentLanguage isEqualToString:@"en"]) {
        path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
        if (path) {
            NSBundle *englishBundle = [NSBundle bundleWithPath:path];
            NSString *englishString = [englishBundle localizedStringForKey:key value:nil table:nil];
            if (![englishString isEqualToString:key]) {
                return englishString;
            }
        }
    }
    
    // 4. 都找不到则返回原key
    return key;
}


NSString *getIDFVForKey(void) {
    NSString *idfv = [[UPKeychainManager sharedManager] readStringForKey:identifyVersionKey];
    if (idfv == nil) {
        return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    }
    return idfv;
}

void upOpenAppSettings(void) {
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
    }
}
